package com.ruoyi.web.controller.order;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WaterWorkOrder;
import com.ruoyi.system.service.IWaterWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工单Controller
 * 
 * @author ruoyi
 * @date 2025-09-16
 * 此Controller为第一个版本的工单
 */
@RestController
@RequestMapping("/service/workOrder")
public class WaterWorkOrderController extends BaseController
{
    @Autowired
    private IWaterWorkOrderService waterWorkOrderService;

    /**
     * 查询工单列表
     */
    @PreAuthorize("@ss.hasPermi('service:workOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(WaterWorkOrder waterWorkOrder)
    {
        startPage();
        List<WaterWorkOrder> list = waterWorkOrderService.selectWaterWorkOrderList(waterWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单列表
     */
    @PreAuthorize("@ss.hasPermi('service:workOrder:export')")
    @Log(title = "工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WaterWorkOrder waterWorkOrder)
    {
        List<WaterWorkOrder> list = waterWorkOrderService.selectWaterWorkOrderList(waterWorkOrder);
        ExcelUtil<WaterWorkOrder> util = new ExcelUtil<WaterWorkOrder>(WaterWorkOrder.class);
        util.exportExcel(response, list, "工单数据");
    }

    /**
     * 获取工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('service:workOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(waterWorkOrderService.selectWaterWorkOrderByOrderId(orderId));
    }

    /**
     * 新增工单
     */
    @PreAuthorize("@ss.hasPermi('service:workOrder:add')")
    @Log(title = "工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WaterWorkOrder waterWorkOrder)
    {
        waterWorkOrder.setStatus(1L);
        waterWorkOrder.setOrderNumber(waterWorkOrderService.getMaxOrderNumber());
        return toAjax(waterWorkOrderService.insertWaterWorkOrder(waterWorkOrder));
    }

    /**
     * 修改工单
     */
    @PreAuthorize("@ss.hasPermi('service:workOrder:edit')")
    @Log(title = "工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WaterWorkOrder waterWorkOrder)
    {
        return toAjax(waterWorkOrderService.updateWaterWorkOrder(waterWorkOrder));
    }


    /**
     * 修改工单 按 order_number
     */
    @PreAuthorize("@ss.hasPermi('service:workOrder:edit')")
    @Log(title = "工单", businessType = BusinessType.UPDATE)
    @PutMapping("editByOrderNumber")
    public AjaxResult editByOrderNumber(@RequestBody WaterWorkOrder waterWorkOrder)
    {
        return toAjax(waterWorkOrderService.updateWaterWorkOrderNumber(waterWorkOrder));
    }

    /**
     * 删除工单
     */
    @PreAuthorize("@ss.hasPermi('service:workOrder:remove')")
    @Log(title = "工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(waterWorkOrderService.deleteWaterWorkOrderByOrderIds(orderIds));
    }
}
